capture log close
log using $log/AED_dataprep_02_outcome_monthly, text replace
   
clear
set matsize 800
set more off

set trace off
   
/* ----------------------------------------------------------------------------- */
/* --- Generating monthly outcome variables based on weekly outcome variables -- */
/* ----------------------------------------------------------------------------- */

local iebversion v901


forvalues year= 1996/1998  {

	use $data/outcome_weekly_y`year'_`iebversion', clear
	

	foreach quelle in beh asu leh lhg bet azeit {

		foreach month in 01 02 03 04 05 06 07 08 09 10 11 12 {
		
		
		di "### Employment status `year' `month' `quelle'"
		di c(current_time)
		
		
			/*Generating monthly indicator*/
			qui gen iza_`quelle'_`year'`month'=9999
	
			/*First, all four weeks within one month have same employment info*/
			qui egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0
			drop help

			/*Second, three out of four weeks within one month have same employment info*/
			qui egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'==9999
			drop help

			qui egen help = diff(iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'2 if help == 0 & iza_`quelle'_`year'`month'==9999
			drop help

			qui egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'==9999
			drop help

			qui egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'4)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'==9999
			drop help


			/*Third, two out of four weeks within one month have same employment info and non-missing*/
			qui egen help_miss = rowmiss(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
	
			qui egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'2)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'==9999 & help_miss<=2 & iza_`quelle'_`year'`month'1~=.
			drop help
			
			qui egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'3)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'==9999 & help_miss<=2 & iza_`quelle'_`year'`month'1~=.
			drop help
			
			qui egen help = diff(iza_`quelle'_`year'`month'1 iza_`quelle'_`year'`month'4)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'1 if help == 0 & iza_`quelle'_`year'`month'==9999 & help_miss<=2 & iza_`quelle'_`year'`month'1~=.
			drop help
			
			qui egen help = diff(iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'3)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'2 if help == 0 & iza_`quelle'_`year'`month'==9999 & help_miss<=2 & iza_`quelle'_`year'`month'2~=.
			drop help
			
			qui egen help = diff(iza_`quelle'_`year'`month'2 iza_`quelle'_`year'`month'4)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'2 if help == 0 & iza_`quelle'_`year'`month'==9999 & help_miss<=2 & iza_`quelle'_`year'`month'2~=.
			drop help
			
			qui egen help = diff(iza_`quelle'_`year'`month'3 iza_`quelle'_`year'`month'4)
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'3 if help == 0 & iza_`quelle'_`year'`month'==9999 & help_miss<=2 & iza_`quelle'_`year'`month'3~=.
			drop help
			
			
			/*Fourth, month gets out-of-labour force if two weeks have a missing, and the other two weeks have different employment info*/
			qui replace iza_`quelle'_`year'`month' = . if help_miss == 2 & iza_`quelle'_`year'`month'==9999
			
			drop help_miss

			/*Fifth, now we have only months left with 4 different employment infos. We choose the one in the middle of the month to dominate*/
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'2 if iza_`quelle'_`year'`month'2~=. & iza_`quelle'_`year'`month'==9999
			qui replace iza_`quelle'_`year'`month' = iza_`quelle'_`year'`month'3 if iza_`quelle'_`year'`month'3~=. & iza_`quelle'_`year'`month'==9999
			
			
			qui replace iza_`quelle'_`year'`month' = . if iza_`quelle'_`year'`month'==9999
			
			/*weekly infos are removed*/
			drop iza_`quelle'_`year'`month'1
			drop iza_`quelle'_`year'`month'2
			drop iza_`quelle'_`year'`month'3
			drop iza_`quelle'_`year'`month'4
			
			
		}/*end:month*/
	}/*end:quelle*/



	/*Now, the earnings information from different sources*/

	foreach quelle in beh leh {

		foreach month in 01 02 03 04 05 06 07 08 09 10 11 12 {
		
		
		di "### Daily earnings `year' `month' `quelle'"
		di c(current_time)


			/*generating monthly indicator*/
			#delimit;
			qui egen iza_geld_`quelle'_`year'`month' = rowmean(iza_geld_`quelle'_`year'`month'1
								  iza_geld_`quelle'_`year'`month'2
								  iza_geld_`quelle'_`year'`month'3
								  iza_geld_`quelle'_`year'`month'4)
			;
			#delimit cr
			
			if "`quelle'" ~= "lgh" {
				/*Daily earning = 0 if no employment info for this month*/
				qui replace iza_geld_`quelle'_`year'`month'=. if iza_`quelle'_`year'`month'==.
			}
			
			/*weekly infos are removed*/
			drop iza_geld_`quelle'_`year'`month'1
			drop iza_geld_`quelle'_`year'`month'2
			drop iza_geld_`quelle'_`year'`month'3
			drop iza_geld_`quelle'_`year'`month'4
			
				
		}/*end:month*/
	}/*end:quelle*/

	compress
	save $data/outcome_monthly_y`year'_`iebversion', replace
	erase $data/outcome_weekly_y`year'_`iebversion'.dta
}/*end:year*/



log close			


